/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-10-06 21:08:50
 * Last Modified: 2016-12-30 10:05:19
 * File Name: controllers/account.go
 * Description:
 */
package controllers

import (
	"encoding/json"

	"passport/account"
	"passport/auth"
	"passport/avatar"
	"passport/errorcode"
	"passport/login"
	"passport/user"

	log "github.com/cihub/seelog"
)

type AccountController struct {
	//beego.Controller
	URLSignController
}

/*
	第三方账号登陆
*/
func (p *AccountController) ThirdAccountLogin() {
	defer p.Output("ThirdAccountLogin")

	body := p.Ctx.Input.RequestBody
	log.Infof("recv body: %s", string(body))
	var reqBody login.ThirdAccountLoginReq
	if err := json.Unmarshal(body, &reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	reqBody.RegIp = p.Ctx.Input.IP()
	reqBody.RegPlat = auth.GetAuthAppKeyService().GetPlat(p.AppKey)
	reqBody.AKId = auth.GetAuthAppKeyService().GetAKId(p.AppKey)

	var code uint
	var msg string
	var respBody *login.LoginResp
	log.Infof("reqBody:%s",reqBody)
	if reqBody.AKId == 1 {
		code, msg, respBody = login.GetLoginService().ThirdAccountLogin(&reqBody)
	} else {
		code, msg, respBody = login.GetLoginService().ThirdAccountLoginBindMobile(&reqBody)
	}
	p.Resp["code"] = code
	p.Resp["msg"] = msg
	if errorcode.EC_GL_SUCCESS == code {
		p.Resp["user_id"] = respBody.Userid
		p.Resp["access_token"] = respBody.AccessToken
		p.Resp["expire_second"] = respBody.ExpireSecond
		p.Resp["hx_account"] = respBody.HXAccount
		p.Resp["hx_passwd"] = respBody.HXPasswd
	}
}

/*
	验证Token是否有效
*/
func (p *AccountController) ValidateAccessToken() {
	defer p.Output("ValidateAccessToken")
	accessToken := p.Ctx.Input.Query("ak")

	if userid, expireSecond, exists := account.GetAccessTokenService().GetUseridByAccessToken(accessToken); exists {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["userid"] = userid
		p.Resp["expire_second"] = expireSecond
		return
	}
	p.Resp["code"] = errorcode.EC_GL_INVALID_ACCESS_TOKEN
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_ACCESS_TOKEN)
}

/*
	手机密码账号登陆
*/
func (p *AccountController) MobilePassLogin() {
	defer p.Output("MobilePassLogin")

	body := p.Ctx.Input.RequestBody
	var reqBody login.MobileLoginReq
	if err := json.Unmarshal(body, &reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	reqBody.AKId = auth.GetAuthAppKeyService().GetAKId(p.AppKey)

	code, msg, respBody := login.GetLoginService().MobilePassLogin(&reqBody)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
	if errorcode.EC_GL_SUCCESS == code {
		p.Resp["user_id"] = respBody.Userid
		p.Resp["access_token"] = respBody.AccessToken
		p.Resp["expire_second"] = respBody.ExpireSecond
		p.Resp["hx_account"] = respBody.HXAccount
		p.Resp["hx_passwd"] = respBody.HXPasswd
	}
}

/*
	按账号查询用户信息
*/
func (p *AccountController) QueryUserInfoByAccount() {
	defer p.Output("QueryUserInfoByAccount")
	accountType := p.GetString("account_type")
	accountId := p.GetString("account")
	userid, useridErr := account.GetAccountService().GetUseridFromAccount(accountType, accountId)
	if useridErr != nil {
		log.Errorf("query user profile failed for account_type: %s, accountId: %s", accountType, accountId)
		p.Resp["code"] = errorcode.EC_LOGIN_ACCOUNT_NOT_EXISTS
		p.Resp["msg"] = "账号不存在"
		return
	}
	ui, uiErr := user.GetUserService().GetAllUserInfo(userid)
	if uiErr != nil {
		log.Errorf("query user profile failed for uid: %s, msg: %s", userid, uiErr.Error())
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
		return
	}
	if ui == nil {
		log.Errorf("query user profile get empty for uid: %s", userid)
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = "该用户不存在"
		return
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	p.Resp["userid"] = userid
	p.Resp["mobile"] = ui.Mobile
	p.Resp["nick_name"] = ui.NickName
	p.Resp["gender"] = ui.Gender
	if len(ui.Avatar) == 32 { //自有的
		p.Resp["avatar"], _ = avatar.GetAvatarURL(ui.Avatar)
	} else { //第三方地址
		p.Resp["avatar"] = ui.Avatar
	}
	p.Resp["slogan"] = ui.Slogan
	p.Resp["region_code"] = ui.RegionCode
	p.Resp["region_addr"] = ui.RegionAddr
	p.Resp["birthday"] = ui.Birthday
	p.Resp["invite_code"] = ui.InviteCode
	p.Resp["invite_by"] = ui.InviteBy
	p.Resp["vuid"] = ui.Vuid
	p.Resp["reg_time"] = ui.InsertTime
	p.Resp["reg_plat"] = ui.RegPlat
}
